# -*- coding: utf-8 -*-
# 作   者 ：许亦安
# 邮   箱 ：xuyian_dev@foxmail.com
# 创建时间 ：2020/10/22  17:53
# 项目名称 ：HFYG
# 文件名称 ：GetBySelenium.py
# 开发工具 ：PyCharm

# 导入模块
import xlwt
from selenium import webdriver
import time
import xlrd
from xlutils.copy import copy
import os

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

# 身份证姓名
stu_xls_path = "./data/查1.xls"
# 查询结果存放路径
result_path = "./data/准考证查询结果.xls"
# 网址
url = 'http://zk.ahzsks.cn/'


# 取文件
def file_isexist(path):
    # 判断账密文件是否存在
    if os.path.exists(path):
        # 存在
        return True
    else:
        # 不存在
        return False


if __name__ == '__main__':
    start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    if not file_isexist(stu_xls_path):
        print("账密文件不存在，先把  \'考生账密.xlsx\'  放在data文件夹内。")
        exit(0)
    # 获取账密表
    sheet = xlrd.open_workbook(stu_xls_path).sheet_by_index(0)
    # 获取总行数
    total_row = sheet.nrows
    # 结果文件不存在，创建结果文件
    if not file_isexist(result_path):
        workbook = xlwt.Workbook(encoding='utf-8')
        zc_sheet = workbook.add_sheet('结果', cell_overwrite_ok=True)
        log_sheet = workbook.add_sheet('log', cell_overwrite_ok=True)
        # 写正常表头
        zc_sheet.write(0, 0, "身份证号")
        zc_sheet.write(0, 1, "姓名")
        zc_sheet.write(0, 2, "准考证号")
        log_sheet.write(0, 0, 0)
        workbook.save(result_path)
        # 行号游标
        log_start = 1
        zc_cursor = 1
    # 结果文件存在，读取log记录确定开始位置
    else:
        ex_workbook = xlrd.open_workbook(result_path, formatting_info=True)
        workbook = copy(ex_workbook)
        zc_sheet = workbook.get_sheet(0)
        log_sheet = workbook.get_sheet(1)
        log_sheet1 = xlrd.open_workbook(result_path).sheet_by_index(1)
        log_start = int(log_sheet1.row_values(0)[0]) + 1
        # 行号游标
        zc_cursor = log_start

    print("[总人数]：" + str(total_row - 1))
    print("[已完成]：" + str(log_start - 1))

    # 创建浏览器对象
    if log_start < total_row:
        driver = webdriver.Firefox()
    for i in range(log_start, total_row):
        # 每150个数据保存一次
        if i % 150 == 0 and i != log_start:
            workbook.save(result_path)

        # 身份证，姓名
        sfz = sheet.row_values(i)[0]
        name = sheet.row_values(i)[1]
        print("第" + str(i) + "次访问---剩余" + str(total_row - zc_cursor - 1) + "人---起始时间：" + start_time
              + "---当前时间：" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
              + "---身份证号：" + sfz + "---姓名：" + name)
        # 打开登录页
        driver.get(url)
        # 等待加载，限时60秒
        driver.implicitly_wait(60)
        # 到查询页面
        driver.find_element_by_xpath('//*[@id="btnAdmissionTicket"]').click()
        # 身份证
        driver.find_element_by_xpath('//*[@id="txtIdentification"]').send_keys(sfz)
        # 姓名
        driver.find_element_by_xpath('//*[@id="txtName"]').send_keys(name)
        # 合肥
        opt = driver.find_element_by_xpath('//*[@id="lstCity"]')
        Select(opt).select_by_value('100')
        # 查询
        driver.find_element_by_xpath('//*[@id="Button2"]').click()
        # 等待加载，限时60秒
        driver.implicitly_wait(60)

        # 判断账号密码是否正确，不正确会弹出弹窗
        is_present_ = EC.alert_is_present()(driver)
        if is_present_:
            print(is_present_.text)
            zc_sheet.write(zc_cursor, 0, sfz)
            zc_sheet.write(zc_cursor, 1, name)
            zc_sheet.write(zc_cursor, 2, is_present_.text)
            log_sheet.write(0, 0, zc_cursor)
            # workbook.save(result_path)
            zc_cursor += 1
            is_present_.accept()
            continue

        # 获取结果
        text = driver.find_element_by_xpath('//*[@id="lblAdmissionTicket"]').text
        print(text)
        zc_sheet.write(zc_cursor, 0, sfz)
        zc_sheet.write(zc_cursor, 1, name)
        zc_sheet.write(zc_cursor, 2, text)
        log_sheet.write(0, 0, zc_cursor)
        # workbook.save(result_path)
        zc_cursor += 1
        # 退出
        driver.find_element_by_xpath('//*[@id="btnBack"]').click()

    workbook.save(result_path)
